﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;

using GalaktikaERPHelper.AtlantisDataTypeConversions.Extensions;
using GalaktikaERPHelper.AtlantisDataTypeConversions; 

namespace Sample
{
    class Program
    {
        static void Main(string[] args)
        {
            string constring = "Data Source=galexp;Initial Catalog=GalTest;Integrated Security=True";
            using (SqlConnection con = new SqlConnection(constring))
            {
                con.Open();
                string sql = "select top 1 f$nrec, f$atl_lastdate, f$atl_lasttime from dbo.t$katmc;";
                using (SqlCommand cmd = new SqlCommand(sql, con))
                {
                    using (SqlDataReader reader = cmd.ExecuteReader())
                    {
                        while (reader.Read())
                        {
                            byte[] nrec = (Byte[])reader["f$nrec"];
                            int dateAtl = (int)reader["f$atl_lastdate"];
                            int timeAtl = (int)reader["f$atl_lasttime"];

                            Console.WriteLine("Nrec {0} - {1} was updated {2}",
                                nrec.ToHexString(),
                                nrec.ToLong(),
                                AtlConvert.AtlDateTimeToDateTime(dateAtl, timeAtl));

                            Console.WriteLine("Nrec {0} - {1} was updated {2}",
                                AtlConvert.ByteToHexString(nrec),
                                AtlConvert.ByteToLong(AtlConvert.LongToByte(nrec.ToLong())),
                                AtlConvert.AtlDateTimeToDateTime(dateAtl, timeAtl));

                            var dt = DateTime.Now;

                            Console.WriteLine("");
                            Console.WriteLine("Now: {0} -> atldate: {1}", dt.ToShortDateString(), dt.ToAtlDate());
                            Console.WriteLine("atldate: {0} -> datetime: {1}", dt.ToAtlDate(), dt.ToAtlDate().ToDateTime());
                        }
                    }
                }
            }
            Console.ReadLine();
        }
    }
}
